package cn.itzzx.resume.controller;

import cn.itzzx.resume.dto.Resume;
import cn.itzzx.resume.response.DataResponse;
import cn.itzzx.resume.response.Response;
import cn.itzzx.resume.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * TODO
 *
 * @author Martin
 * @since 1.0
 */

@Controller
@RequestMapping("resume")
public class ResumeController {

    @Autowired
    private ResumeService resumeService;

    //查询所有记录
    @RequestMapping("list")
    @ResponseBody
    public DataResponse list() {
        List<Resume> list = resumeService.listAll();
        return DataResponse.builder(0,"获取成功",list.size() ,list);
    }


    //删除
    @RequestMapping(value = "deleteById/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public Response deleteById(@PathVariable Long id){
        resumeService.deleteById(id);
        return Response.builder(200,"登陆成功");
    }

    /**
     * 编辑和新增
     * @param resume
     * @return
     */
    @RequestMapping("saveAndUpdate")
    public String update(Resume resume){
        if(StringUtils.isEmpty(resume.getId())){
            resumeService.save(resume);
        }else{
            resumeService.update(resume);
        }
        return "redirect:/index";
    }

}
